import { useState,useEffect } from "react";
const baseURL = "http://8.142.19.67:8089/api/v1/";

const useHttp=({
    url,
    method='get',
    headers={"Content-Type":"application/json"},
    body,
    watch = []
}:any)=>{
    const [result,setResult] = useState<any>(null);
    const [loading,setLoading] = useState<boolean>(true);
    let options:any;
    if(method.toUpperCase()==='GET')
        options=undefined;
    else{
        options = {
            headers,
            method,
            body:JSON.stringify(body)
        }
    }
   function http() {
        setLoading(true);
      
            fetch(baseURL+url,options).then(response => response.json())
                .then((res)=>{
                    setResult(res)
                }).catch(err=>console.log(err))
                .finally(()=>{setLoading(false)})    
        }
    
    useEffect(()=>http(),watch)
    return {loading,setLoading,result,setResult}
}
export default useHttp